package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());

        dishService.saveWithFlavor(dishDto);

        return R.success("新增菜品成功");
    }
    @GetMapping("/page")
    public R findByPage(Integer page,Integer pageSize,String name){
        Page<DishDto> page1 = dishService.findByPage(page, pageSize, name);
       return R.success(page1);
    }
@DeleteMapping
    public R deleteById(@RequestParam(value = "ids" ,required=false)List<Long> ids){
    R r = dishService.deleteCategory(ids);
    return r;
}
@GetMapping("/{id}")
    public R findById(@PathVariable Long id){
    DishDto dishDto = dishService.findDishDtoById(id);
    return R.success(dishDto);
}
@PutMapping
    public R update(@RequestBody DishDto dishDto){
        dishService.updateDishDto(dishDto);
        return R.success("修改成功");
}
@GetMapping("/list")
    public R findDishByCategoryId(Dish dish){
    List<DishDto> dishDtoList = dishService.findDishByCategoryId(dish);
    return R.success(dishDtoList);
}
@PostMapping("/status/{status}")
    public R updateByStatus(@PathVariable Integer status,Long[] ids){
    dishService.updateByStatus(status,ids);
    return R.success("修改成功");
}

}
